package admin

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type DeviceController struct {
	BaseController
}

func (c *DeviceController) Add() {
	p := map[string]interface{}{}
	p["level"] = 1
	_, c.Data["provinceList"] = service.GetRegionList(p)

	c.Data["typeList"] = models.DeviceType
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/device/add.html"
}

func (c *DeviceController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["school_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择学校", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["type"][0]) <= 0 {
		res = utils.ResJson(0, "请选择类型", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["username"][0]) <= 0 {
		res = utils.ResJson(0, "请输入用户名", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["password"][0]) <= 0 {
		res = utils.ResJson(0, "请输入密码", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["place"][0]) <= 0 {
		res = utils.ResJson(0, "请输入位置", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.AddDevice(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *DeviceController) Edit() {
	id := c.Ctx.Input.Param(":id")

	p := map[string]interface{}{}
	p["level"] = 1
	_, c.Data["provinceList"] = service.GetRegionList(p)

	p = map[string]interface{}{}
	p["convert"] = true
	p["relation"] = true
	p["id"] = id
	record := service.GetDeviceInfo(p)
	c.Data["record"] = record

	if record.School.City.Id > 0 {
		p = map[string]interface{}{}
		p["level"] = 2
		p["parentId"] = record.School.Province.Id
		_, c.Data["cityList"] = service.GetRegionList(p)
	}
	if record.School.District.Id > 0 {
		p = map[string]interface{}{}
		p["level"] = 3
		p["parentId"] = record.School.City.Id
		_, c.Data["districtList"] = service.GetRegionList(p)
	}
	if record.School.Id > 0 {
		p = map[string]interface{}{}
		p["districtId"] = record.School.District.Id
		_, c.Data["schoolList"] = service.GetSchoolList(p)
	}

	c.Data["typeList"] = models.DeviceType
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/device/edit.html"
}

func (c *DeviceController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["school_id"][0]) <= 0 {
		res = utils.ResJson(0, "请输入选择学校", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["type"][0]) <= 0 {
		res = utils.ResJson(0, "请选择类型", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if input["bind_status"][0] == "0" {
		if len(input["username"][0]) <= 0 {
			res = utils.ResJson(0, "请输入用户名", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
		if len(input["password"][0]) <= 0 {
			res = utils.ResJson(0, "请输入密码", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	if len(input["place"][0]) <= 0 {
		res = utils.ResJson(0, "请输入位置", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.EditDevice(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *DeviceController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")
	p := map[string]interface{}{}
	provinceId := c.GetString("province_id")
	if len(provinceId) > 0 {
		c.Data["provinceId"] = utils.Atoi64(provinceId)
	} else {
		c.Data["provinceId"] = -1
	}
	cityId := c.GetString("city_id")
	if len(cityId) > 0 {
		c.Data["cityId"] = utils.Atoi64(cityId)
	} else {
		c.Data["cityId"] = -1
	}
	districtId := c.GetString("district_id")
	if len(districtId) > 0 {
		c.Data["districtId"] = utils.Atoi64(districtId)
	} else {
		c.Data["districtId"] = -1
	}
	schoolId := c.GetString("school_id")
	if len(schoolId) > 0 {
		p["schoolId"] = schoolId
		c.Data["schoolId"] = utils.Atoi64(schoolId)
	} else {
		c.Data["schoolId"] = -1
	}
	mType := c.GetString("type")
	if len(mType) > 0 {
		p["type"] = mType
		c.Data["type"], _ = strconv.Atoi(mType)
	} else {
		c.Data["type"] = -1
	}
	schoolName := c.GetString("school_name")
	c.Data["schoolName"] = schoolName
	if len(schoolName) > 0 {
		p["schoolName"] = schoolName
	}
	p["relation"] = true
	p["convert"] = true
	p["curPage"] = curPage
	p["perCount"] = perCount
	totalCount, recordList := service.GetDeviceList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList

	p = map[string]interface{}{}
	p["level"] = 1
	_, c.Data["provinceList"] = service.GetRegionList(p)
	if len(provinceId) > 0 {
		p = map[string]interface{}{}
		p["level"] = 2
		p["parentId"] = provinceId
		_, c.Data["cityList"] = service.GetRegionList(p)
	}
	if len(cityId) > 0 {
		p = map[string]interface{}{}
		p["level"] = 3
		p["parentId"] = cityId
		_, c.Data["districtList"] = service.GetRegionList(p)
	}
	if len(districtId) > 0 {
		p = map[string]interface{}{}
		p["districtId"] = districtId
		_, c.Data["schoolList"] = service.GetSchoolList(p)
	}

	c.Data["typeList"] = models.DeviceType
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/device/list.html"
}